package com.shop.controller;

import com.shop.commom.enums.PayMethod;
import com.shop.commom.utils.CookieUtils;
import com.shop.commom.utils.JSONResult;
import com.shop.commom.utils.Result;
import com.shop.pojo.OrderStatus;
import com.shop.pojo.Orders;
import com.shop.pojo.vo.MerchantOrdersVO;
import com.shop.pojo.vo.OrderVO;
import com.shop.service.OrdersService;
import com.shop.vo.JSONResponse;
import com.shop.vo.OrdersSaveRequest;
import com.shop.vo.OrdersUpdateRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 订单表;
 *
 * @author yukangkang
 * @email sunlightcs@gmail.com
 * @date 2020-10-30 13:16:29
 */
@RestController
@RequestMapping("orders")
public class OrdersController  extends BaseController{

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private RestTemplate restTemplate;



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "详情")
    public JSONResponse info(@PathVariable("id") String id){
		Orders orders = ordersService.getById(id);
        return JSONResponse.ok(orders);
    }

    /**
     * 保存
     */
    @PostMapping("/create")
    @ApiOperation(value = "保存")
    public JSONResult save(@RequestBody OrdersSaveRequest request, HttpServletRequest servletRequest, HttpServletResponse response){
        //创建订单
        if(request.getPayMethod()!= PayMethod.WECHAT.type&&request.getPayMethod()!=PayMethod.ALIPAY.type){
            return JSONResult.error("不支持的支付方式");
        }
        final OrderVO order = ordersService.createOrder(request);
        // todo 一处购物车
        CookieUtils.deleteCookie(servletRequest,response,"shopcart");
        // 像支付中心发送订单

        MerchantOrdersVO merchantOrdersVO = order.getMerchantOrdersVO();
        merchantOrdersVO.setReturnUrl(payReturnUrl);
        // 为了方便测试购买，所以所有的支付金额都统一改为1分钱
        merchantOrdersVO.setAmount(1);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("xxxUserId","xxx");
        headers.add("password","xxx");
        HttpEntity<MerchantOrdersVO> entity =
                new HttpEntity<>(merchantOrdersVO, headers);
        ResponseEntity<Result> responseEntity =
                restTemplate.postForEntity(paymentUrl, entity, Result.class);
        Result paymentResult = responseEntity.getBody();
        if (paymentResult!=null && paymentResult.getStatus() != 200) {
            return JSONResult.error("支付中心订单创建失败，请联系管理员！");
        }

        return JSONResult.OK(order.getOrderId());
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public JSONResponse update(@RequestBody OrdersUpdateRequest request){
		ordersService.update(request);
        return JSONResponse.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public JSONResponse delete(@PathVariable String id){
		ordersService.delete(id);
        return JSONResponse.ok();
    }

    /**
     *支付成功的回调接口
     * @return
     */
    @PostMapping("/notifyMerChantOrderPaid")
    public Integer notifyMerChantOrderPaid(String merChantOrderId){
        ordersService.updateOrderStatus(merChantOrderId);
        return HttpStatus.OK.value();
    }

    @PostMapping("/getPaidOrderInfo")
    public Result getPaidOrderInfo(String orderId) {
        OrderStatus orderStatus = ordersService.queryOrderStatusInfo(orderId);
        return Result.ok(orderStatus);
    }


}
